/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.virtual.model.jcr;

import java.io.OutputStream;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import offset.nodes.client.model.StringArrayConverter;
import offset.nodes.client.model.XmlCharacterFilter;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 *
 * @author Walter Lütgenau
 */
 public class NodeWriter {
    Node node;
    
    public NodeWriter(Node node, ContentHandler handler) {
        this.node = node;
        this.handler = handler;
    }
    
    ContentHandler handler;

    protected void endElement(String name) throws SAXException {
        handler.endElement("", "", name);
    }

    /**
     * Print the node and its attributes, including the child nodes
     *
     * @param node the node
     * @throws org.xml.sax.SAXException
     */
    protected void startElement(String name, Attributes atts) throws SAXException {
        handler.startElement("", "", name, atts);
    }

    protected void addAttribute(AttributesImpl atts, String name, String value) throws SAXException {
        atts.addAttribute("", "", name, "CDATA", XmlCharacterFilter.filterAttributeValue(value));
    }
    
    public void writeNode(Node node) throws RepositoryException, SAXException {
        AttributesImpl atts = new AttributesImpl();
        PropertyIterator pi = node.getProperties();
        while(pi.hasNext()) {
            Property property = pi.nextProperty();
            if ( property.getDefinition() != null && property.getDefinition().isMultiple() ) {
                StringArrayConverter sa = new StringArrayConverter(property.getValues());
                String value = sa.toString();
                if ( value != null && value.length() > 0)
                    addAttribute(atts, property.getName(), sa.toString());
            } /*else if (property.getDefinition().getRequiredType() == PropertyType.BINARY)
                continue; */// ignore binary properties
            else if ( property.getString() != null && property.getString().length() > 0)
                addAttribute(atts, property.getName(), property.getString());
        }

        String name = XmlNameConverter.encodeName(node.getName());
        startElement(name, atts);
        
        NodeIterator ni = node.getNodes();
        while(ni.hasNext()) {
            Node child = ni.nextNode();
            writeNode(child);
        }
        
        endElement(name);
    }

    public void write() throws SAXException, RepositoryException  {
        handler.startDocument();
        writeNode(node);
        handler.endDocument();
    }
}
